package com.ruoyi.consumer;


import com.ruoyi.config.RabbitMQConfig;
import com.ruoyi.netty.ElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.support.Acknowledgment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;


/**
 * @author hlf
 * @createTime 2025/2/6 16:42
 * @description
 */
@Service
@Slf4j
@ConditionalOnProperty(name = "spring.rabbitmq.enabled", havingValue = "true", matchIfMissing = false)
public class RabbitMQConsumer {



    @Autowired
    private ElasticSearchService elasticSearchService;

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(Message<String> message) throws InterruptedException {
        log.info("Received message: " + message.getPayload());
        if (true) {
            throw new RuntimeException("故意的");
        }
        //acknowledgment.acknowledge(); // 手动确认消息
        log.info("消息处理完成");
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME_ARTICLE)
    public void articleMessage(Message<String> message) throws InterruptedException {
        log.info("Received message: " + message.getPayload());
        elasticSearchService.articleAddEs(message.getPayload());
        //acknowledgment.acknowledge(); // 手动确认消息
        log.info("消息处理完成");
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME_ARTICLE_ALL)
    public void articleMessageAll(Message<String> message) throws InterruptedException {
        log.info("Received message: " + message.getPayload());
        elasticSearchService.articleMessageAll(message.getPayload());
        //acknowledgment.acknowledge(); // 手动确认消息
        log.info("消息处理完成");
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME_ARTICLE_DEL)
    public void articleMessageDel(Message<String> message) throws InterruptedException {
        log.info("Received message: " + message.getPayload());
        elasticSearchService.articleMessageDel(message.getPayload());
        //acknowledgment.acknowledge(); // 手动确认消息
        log.info("消息处理完成");
    }


}

